﻿@*------------------------------------------------------------
  Copyright (c) Microsoft Corporation.  All rights reserved.
  Licensed under the MIT License (MIT). See License.txt in the repo root for license information.
------------------------------------------------------------*@

@page "/applications"

@using Microsoft.AspNetCore.Components;
@using Microsoft.Azure.IIoT.App.Common;
@using Microsoft.Azure.IIoT.App.Components.Buttons;
@using Microsoft.Azure.IIoT.App.Components.Drawer;
@using Microsoft.Azure.IIoT.App.Components.ErrorMessage;
@using Microsoft.Azure.IIoT.App.Components.Loader;
@using Microsoft.Azure.IIoT.App.Models;
@using Microsoft.Azure.IIoT.App.Services;
@using Microsoft.Azure.IIoT.OpcUa.Api.Registry;
@using Microsoft.Azure.IIoT.OpcUa.Api.Registry.Models;

@inject NavigationManager NavigationManager
@inject Registry RegistryHelper
@inject IRegistryServiceApi RegistryService
@inject RegistryServiceEvents RegistryServiceEvents
@inject UICommon CommonHelper

@inherits IIoTItemsCollection<ApplicationInfoApiModel>

<h1>Assets</h1>

<table class="table table-responsive @_tableView">
    <thead>
        <tr>
            <th class="width-large">Name</th>
            <th class="width-large">Uri</th>
            <th class="width-medium">Site</th>
            <th class="width-small">Type</th>
            <th class="width-small"></th>
        </tr>
    </thead>
    <tbody>
        @foreach (var application in Items.Results)
        {
            <tr class="@((application.NotSeenSince != null? "enabled-false" : "enabled-true"))">
                <td class="hover-text large">
                    <div>
                        @{
                            CommonHelper.ApplicationUri.AddOrUpdate(application.ApplicationId, application.ApplicationUri);
                        }
                        @if (application.NotSeenSince != null)
                        {
                            @application.ApplicationName
                        }
                        else
                        {
                            <a href='endpoints/@RegistryHelper.PathAll/@application.ApplicationId/@RegistryHelper.PathAll'>
                                @application.ApplicationName
                            </a>
                        }
                    </div>
                </td>
                <td class="hover-text width-large">
                    <div>
                        @application.ApplicationUri
                    </div>
                </td>
                <td class="hover-text width-medium">
                    <div>
                        @application.SiteId
                    </div>
                </td>
                <td class="hover-text width-small">
                    <div>
                        @application.ApplicationType
                    </div>
                </td>
                <td class="hover-text width-small">
                    <div>
                        <a class="ellipsis-space" href="javascript: void(0)" @onclick="@(() => OpenDrawer(application))">More Data</a>
                    </div>
                </td>
                <td>
                    <button @onclick="@(() => UnregisterApplicationUIAsync(application.ApplicationId))" class="btn btn-primary">Unregister</button>
                </td>
            </tr>
        }
    </tbody>
</table>
<div class="@_tableEmpty center">No Assets found.</div>

<Drawer HeaderText="Asset Info" ObjectData="@ApplicationData" IsOpen="@IsOpen" CloseDrawer="@(() => CloseDrawer())">
    <_DrawerAssetContent ApplicationData="@ApplicationData"></_DrawerAssetContent>
</Drawer>

<ErrorMessage PageError="@Items.Error" Status="@null"></ErrorMessage>

<Spinner IsLoading=@IsLoading></Spinner>

<LoadMore Result="@Items" IsLoading=@IsLoading LoadMoreItems=@(async() => await LoadMoreItems()) />